package mysql

import (
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/dto/paginate"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/domain/models"
	mysqlClients "gitee.com/captials-team/ubdframe/src/infrastructure/clients/mysql"
	"gorm.io/gorm"
)

func NewOrganizeExtraDao(conf configstc.DBConfig) *OrganizeExtraDao {
	return &OrganizeExtraDao{
		DBFacade: NewGormDBFacade[models.OrganizeExtra](mysqlClients.NewGormDB(conf)),
		conf:     conf,
	}
}

type OrganizeExtraDao struct {
	interfaces.DBFacade[models.OrganizeExtra]
	conf configstc.DBConfig
}

func (dao *OrganizeExtraDao) Search(orgId int64) ([]*models.OrganizeExtra, error) {
	list, _, err := dao.DBFacade.Search(&paginate.Pager{
		Page: 1,
		Size: 1000,
	}, func(db *gorm.DB) *gorm.DB {
		return db.Where("org_id", orgId)
	})
	return list, err
}

func (dao *OrganizeExtraDao) Clear(orgId int64) (int64, error) {
	return dao.DBFacade.DeleteByScope(func(db *gorm.DB) *gorm.DB {
		return db.Where("org_id", orgId)
	})
}

func (dao *OrganizeExtraDao) Use(db *gorm.DB) interfaces.ItfOrganizeExtra {
	return &OrganizeExtraDao{
		conf:     dao.conf,
		DBFacade: dao.DBFacade.UseDB(db),
	}
}
